<?php

namespace hiduoke\services\upload\driver;


use hiduoke\base\BaseUpload;
use hiduoke\exception\UploadException;
use think\facade\Filesystem;
use think\File;

/**
 * 本地上传 Class Local
 * @package hiduoke\services\upload\driver
 * User: liuhongbin
 * Date: 2021-10-03 18:24
 */
class Local extends BaseUpload
{

    /**
     * 默认存放路径
     * @var string
     */
    protected $defaultPath;
    protected $storageDomain;

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->storageDomain = get_host() . config('filesystem.disks.public.url');
        $this->defaultPath = "";
    }

    protected function app()
    {
        return Filesystem::disk('public');
    }

    /**
     * 生成上传文件目录
     * @param $path
     * @param null $root
     * @return string
     */
    protected function uploadDir($path, $root = null)
    {
        if ($root === null) $root = app()->getRootPath() . 'public/';
        return str_replace('\\', '/', $root . 'uploads/' . $path);
    }

    /**
     * 检查上传目录不存在则生成
     * @param $dir
     * @return bool
     */
    protected function validDir($dir)
    {
        return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
    }

    /**
     * 文件上传
     * @param string $fileName
     * @return array|bool|mixed|\StdClass
     */
    public function upload(string $fileName = 'file')
    {
        $fileHandle = app()->request->file($fileName);
        if (!$fileHandle) {
            throw new UploadException("上传配置有误");
        }
        $this->validateCheck($fileName, $fileHandle);

        //这里得到的是相对路径
        $filePath = $this->app()->putFile($this->path, $fileHandle);
        if (!$filePath) {
            throw new UploadException('上传失败');
        }
        //这里得到的是完整路径
        $_filePath = $this->app()->path($filePath);
        $this->fileInfo->uploadInfo = new File($_filePath);
        $this->fileInfo->filePath = $filePath;
        $this->fileInfo->fileUrl = $this->storageDomain . '/' . $filePath;
        $this->fileInfo->oldName = $fileHandle->getOriginalName();
        $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
        return $this->fileInfo;
    }

    /**
     * 文件流上传
     * @param string $fileContent
     * @param string|null $key
     * @return array|bool|mixed|\StdClass
     */
    public function stream(string $fileContent, string $key = null)
    {
        if (!$key) {
            $key = $this->createFileName();
        }
        $dir = $this->uploadDir($this->path);
        if (!$this->validDir($dir)) {
            throw new UploadException("上传配置有误");
        }
        $fileName = $dir . '/' . $key;
        file_put_contents($fileName, $fileContent);
        $this->fileInfo->uploadInfo = new File($fileName);
        $this->fileInfo->realName = $key;
        $this->fileInfo->fileName = $key;
        $this->fileInfo->filePath = $this->defaultPath . '/' . $this->path . '/' . $key;
        return $this->fileInfo;
    }

    /**
     * 删除文件
     * @param string $filePath
     * @return bool|mixed
     */
    public function delete(string $filePath)
    {
        $_filePath = Filesystem::disk('public')->path($filePath);
        if (file_exists($_filePath)) {
            unlink($_filePath);
            return true;
        }
        return false;
    }

    public function save(string $file = 'file')
    {
        return $this->upload($file);
    }
}
